﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI.WebControls;
using consultasSql.consultasVarias;


public partial class Stdby_Busqueda_selectIdRegistroOt : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cargarGrilla("");
            ViewState["ordenarGrilla"] = "ASC";
            Session["filaSeleccionada"] = "-1";
        }
    }

    protected void Grilla_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
        }
    }


    //cargo grilla
    private void cargarGrilla(string ordenar)
    {
        try
        {
            Grilla.DataSource = darDataSet(ordenar);
            Grilla.DataBind();
        }
        catch { }
    }


    private DataSet darDataSet(string ord)
    {
        

        string IdRemCab = txtBuscaIdRemito.Text.Trim();

        string Distribuidora = txtBuscaDistribuidora.Text.Trim();


        string consRem = null;

        if (!String.IsNullOrEmpty(IdRemCab))
        {
            if (IdRemCab.Substring(0, 1) == "*")
            {
                consRem = " RTRIM(LTRIM(IdRemCab)) LIKE '%' + @IdRemCab";
            }
            else if (IdRemCab.Substring(IdRemCab.Length - 1, 1) == "*")
            {
                consRem = " RTRIM(LTRIM(IdRemCab)) LIKE @IdRemCab + '%'";
            }
            else
            {
                consRem = " RTRIM(LTRIM(IdRemCab)) LIKE @IdRemCab";
            }
        }

        string consDist = null;

        if (!String.IsNullOrEmpty(Distribuidora))
        {
            if (Distribuidora.Substring(0, 1) == "*")
            {
                consDist += " RTRIM(LTRIM(Distribuidora)) LIKE '%' + @Distribuidora";
            }
            else if (Distribuidora.Substring(Distribuidora.Length - 1, 1) == "*")
            {
                consDist += " RTRIM(LTRIM(Distribuidora)) LIKE @Distribuidora + '%'";
            }
            else
            {
                consDist += " RTRIM(LTRIM(Distribuidora)) LIKE @Distribuidora";
            }
        }


        string cons = "";


        if (!String.IsNullOrEmpty(consRem) && !String.IsNullOrEmpty(consDist))
        {
            cons = "WHERE" + consRem + " AND" + consDist;

        }
        else if (!String.IsNullOrEmpty(consRem) && String.IsNullOrEmpty(consDist))
        {
            cons = "WHERE" + consRem;
        }
        else if (String.IsNullOrEmpty(consRem) && !String.IsNullOrEmpty(consDist))
        {
            cons = "WHERE" + consDist;
        }
        else
        {
            cons = "";
        }



        string ordenar = "";

        if (!String.IsNullOrEmpty(ord.Trim()))
        {
            ordenar = " ORDER BY " + ord.Trim();
        }


        string consulta = "SELECT Id, IdRemCab, Distribuidora, Requisicion, NroFC, TipoFC, CASE WHEN Anulada = 1 THEN 'ANULADA' ELSE NULL END AS 'Anulada' FROM OTCab " + cons + ordenar;

        SqlCommand command = new SqlCommand();
        command.CommandType = CommandType.Text;
        command.CommandText = consulta;

        command.Parameters.Clear();

        if (!String.IsNullOrEmpty(IdRemCab))
        {
            command.Parameters.AddWithValue("@IdRemCab", IdRemCab.Replace("*", ""));
        }

        if (!String.IsNullOrEmpty(Distribuidora))
        {
            command.Parameters.AddWithValue("@Distribuidora", Distribuidora.Replace("*", ""));
        }

        return new CDarDataSet().darDataSet(command);
    }
    

    //cambio de pagina grilla
    protected void grilla_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            Grilla.PageIndex = e.NewPageIndex;
            cargarGrilla("");
        }
        catch { }
    }


    //ordenar grilla
    protected void Grilla_Sorting(object sender, GridViewSortEventArgs e)
    {
        try
        {
            cargarGrilla(e.SortExpression + " " + ordenarGrilla());
        }
        catch { }
    }

    private string ordenarGrilla()
    {
        try
        {
            if (!String.IsNullOrEmpty(ViewState["ordenarGrilla"].ToString()))
            {
                if (ViewState["ordenarGrilla"].ToString() == "DESC")
                {
                    ViewState["ordenarGrilla"] = "ASC";
                }
                else
                {
                    ViewState["ordenarGrilla"] = "DESC";
                }

            }
            else
            {
                ViewState["ordenarGrilla"] = "ASC";
            }
        }
        catch { }

        return ViewState["ordenarGrilla"].ToString();
    }

    //campo seleccionado
    protected void Grilla_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {

            if (!(Convert.ToString(Session["filaSeleccionada"]) == Convert.ToString(Grilla.SelectedIndex)))
            {
                Session["filaSeleccionada"] = Grilla.SelectedIndex.ToString();
                cargarCampos();
            }
            else
            {
                limpiarCampos();
                Grilla.SelectedIndex = -1;
                Session["filaSeleccionada"] = -1;
            }

        }
        catch { }
    }


    private void cargarCampos()
    {
        try
        {
            if (Grilla.SelectedRow != null)
            {
                GridViewRow grilla = Grilla.SelectedRow;

                txtOt.Text = HttpUtility.HtmlDecode(grilla.Cells[1].Text);
                txtIdRemito.Text = HttpUtility.HtmlDecode(grilla.Cells[2].Text);
            }
        }
        catch { }
    }

    private void limpiarCampos()
    {
        try
        {
            txtOt.Text = "";
            txtIdRemito.Text = "";
        }
        catch { }
    }

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        cargarGrilla("");
    }

    
}